<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.mongodb.client.MongoCursor,org.bson.Document" %>
<%@ page import="mongo.TeachingMaterialsType" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加教学材料模板</title>
</head>
<body>
	<jsp:include page="../navbar.html" />
	<div class="container-fluid">
		<div class="row">
			<div class="col-md-1 col-md-offset-3">
				<a href="teachingMaterialsTemplateServlet?method=list">
					<button type="button" class="btn btn-default"><em class="fa fa-chevron-left"></em> 返回</button>
				</a>
			</div>
		</div>
    	<div class="row mt20">
			<div class="col-md-6 col-md-offset-3">
				<div class="panel">
                	<div class="panel-body">
                  		<h4 class="page-header mt0">教学材料模板</h4>
                 		<form name="templateform" role="form" action="/jspbighomework/manager/userManage/UploadServlet" method="post" enctype="multipart/form-data" onsubmit="return check(this);" class="form-horizontal mt15">
	                  	    <fieldset>
				              <div class="form-group" style="margin-left:40px">
				                  	<a href="javascript:;" class="file">选择文件
								     	<input type="file" name="file" />
								  	</a>
						            <span class="showFileName">
						             	未选择任何文件
						            </span>
				               </div> 
			                </fieldset>
		               		<fieldset>
			               		<div class="form-group mt15">
			                  		<label class="col-md-2 control-label">所属类型</label>
		                  			<div class="col-md-9">
		                     			<select class="form-control" name="typeid" id="selectType">
						                <%
						                    TeachingMaterialsType TeachingMaterialsType = new TeachingMaterialsType();
						               	    MongoCursor<Document> result = TeachingMaterialsType.list();
					                  		while(result.hasNext()) {
					                  			Document document = result.next();%>
					                     	<option value="<%=document.getObjectId("_id") %>"><%=document.getString("name") %></option>
					                    <%} %> 
					                  </select>
		                  			</div>
		               			</div>	
		               		</fieldset> 	
		               		<input type="hidden" name="typename" id="typename">                		          
		               		<div class="col-md-10 col-md-offset-1 mt20">
		               			<div class="col-md-6">
		               				<button type="submit" class="btn btn-primary btn-block" name="template">添加</button> 
		               			</div>
		               			<div class="col-md-6">
		               				<button type="reset" class="btn btn-default btn-block">重置</button> 
		               			</div>
		               		</div>	               		
              			</form>
               		</div>
            	</div>
         	</div>
       </div>
    </div>
    <script>
	    var  Sys = {};
	    if(navigator.userAgent.indexOf("MSIE")>0)
	    {
	        Sys.ie=true;
	    }
	    if(isFirefox=navigator.userAgent.indexOf("Firefox")>0)
	    {
	        Sys.firefox=true;
	    }
	     
		//表单提交前进行数据验证，数据合法则返回true表单提交
		function check(templateform) {  
			var myselect = document.getElementById("selectType");
		    var index = myselect.selectedIndex ;             // selectedIndex代表的是你所选中项的index
		    templateform.typename.value =  myselect.options[index].text; //拿到选中项options的text
		   
		    if (templateform.file.value == "") {
	            swal("请选择要上传的文件","", "warning");
	            return false;
	        }

		    return true;  
		} 
		
		$(document).ready(function(){
			$(".file").on("change","input[type='file']",function(){
			    var filePath=$(this).val();
			    if(filePath!=null || filePath!=""){
			        var arr=filePath.split('\\');
			        var fileName=arr[arr.length-1];
			        $(".showFileName").text(fileName);
			    }else{
			        $(".showFileName").text("未选择任何文件");
			        return false; 
			    }
			})
		});
	</script>
</body>
</html>